﻿package mortal.game.view.guild.buildingPanel
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import extend.language.*;
    import fl.data.*;
    import flash.events.*;
    import flash.text.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.cache.guild.module.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.guild.cellrender.*;
    import mortal.game.view.guild.otherpanel.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuildWareHousePanel extends GuildBuildingBasePanel
    {
        private var _tabBar:GTabarNew;
        private var _tabDataArr:Array;
        private var _txtCapacity:GTextFiled;
        private var _bg:ScaleBitmap;
        private var _list:GTileList;
        private var _pageSelecter:PageSelecter;
        private var _goldItem:MoneyItem2;
        private var _coinItem:MoneyItem2;
        private var _btnApplyMoney:GButton;
        private var _btnDonateMoney:GButton;
        private var _btnMyApplyList:GButton;
        private var _btnDealApply:GButton;
        private var _btnRecord:GButton;
        private var _btnResourceAllot:GButton;
        private var _txtAllApplyNum:GTextFiled;
        private var _bgAllApplyNum:GBitmap;
        private static var _instance:GuildWareHousePanel;

        public function GuildWareHousePanel(param1:ILayer = null)
        {
            setSize(485, 405);
            var _loc_2:* = Language.getArray(71070);
            this._tabDataArr = _loc_2;
            super(param1);
            if (_instance)
            {
                throw new Error("GuildWareHousePanel single");
            }
            title = "" + Language.getString(71071);
            titleHeight = 60;
            return;
        }// end function

        override public function layoutUI() : void
        {
            super.layoutUI();
            var _loc_1:* = GlobalStyle.textFormatHuang;
            _loc_1.align = TextFormatAlign.RIGHT;
            this._txtCapacity = UIFactory.gTextField("", 340, 39, 130, 22, this, _loc_1);
            this._bg = UIFactory.bg(10, 60, 466, 300, this);
            this._pageSelecter = UIFactory.pageSelecter(189, 298, this, PageSelecter.CompleteMode);
            this._pageSelecter.setbgStlye(ImagesConst.ComboBg, new GTextFormat());
            this._pageSelecter.maxPage = 1;
            this._pageSelecter.pageTextBoxSize = 36;
            this._pageSelecter.configEventListener(Event.CHANGE, this.onPageChange);
            this._list = UIFactory.tileList(19, 68, 450, 230, this);
            this._list.rowHeight = 45;
            this._list.columnWidth = 45;
            this._list.rowCount = 5;
            this._list.columnCount = 10;
            this._list.setStyle("cellRenderer", GuildItemCellRenderer);
            this._goldItem = UIFactory.getUICompoment(MoneyItem2, 23, 327, this);
            this._goldItem.setBgName(ImagesConst.InputBg);
            this._goldItem.unit = EPriceUnit._EPriceUnitGold;
            this._coinItem = UIFactory.getUICompoment(MoneyItem2, 162, 327, this);
            this._coinItem.setBgName(ImagesConst.InputBg);
            this._coinItem.unit = EPriceUnit._EPriceUnitCoin;
            this._btnApplyMoney = UIFactory.gButton("" + Language.getString(71072), 300, 326, 67, 24, this);
            this._btnDonateMoney = UIFactory.gButton("" + Language.getString(71073), 380, 326, 67, 24, this);
            this._btnMyApplyList = UIFactory.gButton("" + Language.getString(71074), 20, 368, 67, 24, this);
            this._btnDealApply = UIFactory.gButton("" + Language.getString(71075), 90, 368, 67, 24, this);
            this._btnRecord = UIFactory.gButton("" + Language.getString(71076), 208, 368, 67, 24, this);
            this._btnResourceAllot = UIFactory.gButton("" + Language.getString(71077), 324, 368, 67, 24, this);
            this._bgAllApplyNum = UIFactory.gBitmap(ImagesConst.NumberBg, 130, 353, this);
            this._bgAllApplyNum.visible = false;
            var _loc_2:* = GlobalStyle.textFormatBai;
            _loc_2.align = TextFormatAlign.CENTER;
            this._txtAllApplyNum = UIFactory.gTextField("99", 130, 353, 24, 22, this, _loc_2);
            this._txtAllApplyNum.visible = false;
            this._tabBar = UIFactory.gTabBarNew(0, 33, this._tabDataArr, 483, 405, 75, 28, this, this.onTabBarChange, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this._tabBar.drawNow();
            this.configEventListener(DragEvent.Event_Move_In, this.itemMoveInHandler);
            Dispatcher.addEventListener(EventName.Guild_WareHouse_ClickPackItem, this.onClickPackItem);
            Cache.instance.guild.selfGuildInfo.wareHouse.isOpen = true;
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_WareHouse_StatusChange));
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guild_wareHouse_getData));
            return;
        }// end function

        private function onClickPackItem(event:DataEvent) : void
        {
            this.startPutInItem(event.data as ItemData);
            return;
        }// end function

        private function itemMoveInHandler(event:DragEvent) : void
        {
            var _loc_2:* = event.dragSouce as ItemData;
            var _loc_3:* = event.dragItem as BaseItem;
            if (_loc_3.parent is GuildResourceAllotRenderer == false && _loc_3.parent is GuildItemCellRenderer == false && _loc_2.serverData.posType == EPlayerItemPosType._EPlayerItemPosTypeBag)
            {
                this.startPutInItem(_loc_2);
            }
            else
            {
                return;
            }
            return;
        }// end function

        private function startPutInItem(param1:ItemData) : void
        {
            GuildDonateItemPanel.instance.showPanel(param1);
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            this.updateList();
            return;
        }// end function

        private function onTabBarChange(event:MuiEvent = null) : void
        {
            this._pageSelecter.currentPage = 1;
            this.updateList();
            return;
        }// end function

        override public function onMouseClick(event:MouseEvent) : void
        {
            super.onMouseClick(event);
            switch(event.target)
            {
                case this._btnRecord:
                {
                    GuildWareHouseRecordPanel.instance.popUp();
                    break;
                }
                case this._btnMyApplyList:
                {
                    GuildWareHouseMyApplyListPanel.instance.popUp();
                    break;
                }
                case this._btnDealApply:
                {
                    GuildWareHouseDealApplyPanel.instance.popUp();
                    break;
                }
                case this._btnApplyMoney:
                {
                    GuildWareHouseApplyMoney.instance.popUp();
                    break;
                }
                case this._btnDonateMoney:
                {
                    GuildDonateGoldPanel.instance.popUp();
                    break;
                }
                case this._btnResourceAllot:
                {
                    if (!GuildConst.hasPermissions(GuildConst.ResourceAllot))
                    {
                        MsgManager.showRollTipsMsg(Language.getString(70535));
                        return;
                    }
                    GameController.pack.view.hide();
                    GuildResourceAllotPanel.instance.show();
                    LayerManager.windowLayer.centerWindows(this, GuildResourceAllotPanel.instance);
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildParticipationInfoReq));
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        override public function update() : void
        {
            super.update();
            this.updateMoneys();
            this.updateList();
            this.updateApplyLabel();
            if (GuildConst.hasPermissions(GuildConst.ManagerWareHouse))
            {
                this._btnApplyMoney.label = "" + Language.getString(71078);
                this._btnDealApply.visible = true;
                this._btnMyApplyList.visible = false;
            }
            else
            {
                this._btnApplyMoney.label = "" + Language.getString(71079);
                this._btnDealApply.visible = false;
                this._btnMyApplyList.visible = true;
            }
            return;
        }// end function

        public function updateMoneys() : void
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo.wareHouse.moneyDic;
            this._coinItem.value = _loc_1[EPriceUnit._EPriceUnitCoin];
            this._goldItem.value = _loc_1[EPriceUnit._EPriceUnitGold];
            return;
        }// end function

        public function updateList() : void
        {
            var _loc_1:* = Cache.instance.guild.selfGuildInfo.wareHouse;
            this._list.dataProvider = this.getDataProvider();
            this._list.drawNow();
            var _loc_2:* = _loc_1.warehouseItemVec.length;
            this._pageSelecter.maxPage = Math.ceil(_loc_1.totalGrid / (this._list.rowCount * this._list.columnCount));
            this._txtCapacity.text = Language.getString(71080) + _loc_2 + "/" + _loc_1.totalGrid;
            return;
        }// end function

        public function updateApplyLabel() : void
        {
            if (GuildConst.hasPermissions(GuildConst.ManagerWareHouse))
            {
                this._txtAllApplyNum.text = Cache.instance.guild.selfGuildInfo.wareHouse.allApplyNum.toString();
                this._txtAllApplyNum.visible = Cache.instance.guild.selfGuildInfo.wareHouse.allApplyNum > 0;
                this._bgAllApplyNum.visible = Cache.instance.guild.selfGuildInfo.wareHouse.allApplyNum > 0;
            }
            return;
        }// end function

        private function getDataProvider() : DataProvider
        {
            var _loc_3:Vector.<SPlayerItem> = null;
            var _loc_1:* = new DataProvider();
            var _loc_2:* = Cache.instance.guild.selfGuildInfo.wareHouse;
            switch(this._tabBar.selectedIndex)
            {
                case 0:
                {
                    _loc_3 = _loc_2.warehouseItemVec;
                    break;
                }
                case 1:
                {
                    _loc_3 = _loc_2.warehouseItemVec.filter(this.isNotEquipment);
                    break;
                }
                case 2:
                {
                    _loc_3 = _loc_2.warehouseItemVec.filter(this.isEquipment);
                    break;
                }
                default:
                {
                    break;
                }
            }
            var _loc_4:* = this._list.rowCount * this._list.columnCount;
            var _loc_5:* = (this._pageSelecter.currentPage - 1) * _loc_4;
            var _loc_6:* = _loc_5 + Math.min(_loc_4, _loc_2.totalGrid - _loc_5);
            var _loc_7:* = _loc_5;
            while (_loc_7 < _loc_6)
            {
                
                if (_loc_7 < _loc_3.length)
                {
                    _loc_1.addItem(_loc_3[_loc_7]);
                }
                else
                {
                    _loc_1.addItem(new Object());
                }
                _loc_7++;
            }
            return _loc_1;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            Cache.instance.guild.selfGuildInfo.wareHouse.isOpen = false;
            _instance = null;
            return;
        }// end function

        override public function hide() : void
        {
            super.hide();
            if (GuildResourceAllotPanel.isShow)
            {
                GuildResourceAllotPanel.instance.hide();
            }
            return;
        }// end function

        private function isEquipment(param1:SPlayerItem, param2:int, param3:Vector.<SPlayerItem>) : Boolean
        {
            return ItemsUtil.isEquip(new ItemData(param1.itemCode));
        }// end function

        private function isNotEquipment(param1:SPlayerItem, param2:int, param3:Vector.<SPlayerItem>) : Boolean
        {
            return !ItemsUtil.isEquip(new ItemData(param1.itemCode));
        }// end function

        public static function get instance() : GuildWareHousePanel
        {
            if (!_instance)
            {
                _instance = new GuildWareHousePanel;
            }
            return _instance;
        }// end function

        public static function hasInstance() : Boolean
        {
            return _instance != null;
        }// end function

    }
}
